package com.wryyyyy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wryyyyy.dto.PageBean;
import com.wryyyyy.dto.Result;
import com.wryyyyy.entity.Material;
import com.wryyyyy.service.IMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wryyyyy
 * @since 2023-11-12
 */
@Slf4j
@RestController
@RequestMapping("/material")
public class MaterialController {
    @Resource
    private IMaterialService materialService;

    /**
     * 管理员添加材料
     * @param material 材料
     * @return 操作结果
     */
    @PostMapping("/save")
    public Result save(@RequestBody Material material) {
        if (!materialService.save(material)) {
            return Result.fail("添加失败-_-!");
        }
        return Result.ok();
    }

    /**
     * 获取全部材料数量
     * @param name 查询关键词
     * @return 数量
     */
    @GetMapping("/count")
    public Result count(
            @RequestParam(value = "name", required = false) String name
    ) {
        Integer count;
        if (Objects.equals(name, "") || name == null) {
            count = materialService.count();
        } else {
            count = materialService.query().like("label_name", name).count();
        }
        return Result.ok(count);
    }


    /**
     * 查看材料详情
     * @param materialId 材料id
     * @return 操作结果
     */
    @GetMapping("/{materialId}")
    public Result queryMaterialById(@PathVariable String materialId) {
        return Result.ok(materialService.getById(materialId));
    }

    /**
     * 删除单个材料
     * @param id 材料id
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id){
        if (!materialService.removeById(id)) {
            return Result.fail("删除失败-_-!");
        }
        return Result.ok();
    }

    /**
     * 批量删除材料
     * @param ids 材料id
     * @return 操作结果
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody String[] ids) {
        if (materialService.removeByIds(Arrays.asList(ids))) {
            return Result.ok();
        }
        return Result.fail("操作失败-_-!");
    }

    /**
     * 修改材料
     * @param material 材料
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody Material material) {
        if (!materialService.updateById(material)) {
            return Result.fail("修改失败-_-!");
        }
        return Result.ok();
    }

    /**
     * 根据 材料名称关键字 分页查询材料信息
     * @param name 材料名称关键字
     * @param current 页码
     * @return 材料列表
     */
    @GetMapping("/list")
    public Result query(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        Page<Material> page;
        Integer count;
        if (Objects.equals(name, "") || name == null) {
            page = materialService.query()
                    .page(new Page<>(current, size));
            count = materialService.count();
        } else {
            page = materialService.query()
                    .like("material_name", name)
                    .page(new Page<>(current, size));
            count = materialService.query().like("material_name", name).count();
        }
        List<Material> records = page.getRecords();
        return Result.ok(new PageBean<>(records, count));
    }
}